<?php
# INIT.PHP
# Initial actions for everypages
# Included in every pages

// Create connection
$DB = new mysql();
$DB->connect($database['server'],$database['user'],$database['pwd'],$database['name']);
$DB->query("SET NAMES 'utf8'");

// Get config from table site_config
$q_config = $DB->query('SELECT * FROM '.$tbl['site_config'].' WHERE active=1');
$num_config = $DB->num_rows($q_config);
if($num_config)
{
	for($x=0;$x<$num_config;$x++)
	{
		$field = $DB->result($q_config,$x,"field");
		$value = $DB->result($q_config,$x,"value");
		//echo $field.'=>'.$value;
		$cfg[$field] = trim($value);
	}
}

// Select language
if($cfg['multi_language'])
{
	$q_language = $DB->query('SELECT * FROM '.$tbl['language'].' WHERE active=1');
	$num_language = $DB->num_rows($q_language);
	for($x=0;$x<$num_language;$x++)
	{
		$language_array = $DB->result_array($q_language,$x,"id,title,code");
		$language[$language_array['id']] = $language_array['code'].','.$language_array['title'];
	}
	if($num_language==1)  $_GET['lang_id'] = 'all';
}

// Define language for SHOW
if($_GET['lang_id'])
{
	if($_GET['lang_id'] == 'all') $_SESSION['admin_show_lang_id'] = false;
	else $_SESSION['admin_show_lang_id'] = $_GET['lang_id'];
}

// Delete expired session and update new session
// Delete time-out session
$admin_timeout = $time - $cfg["session_timeout"];
$DB->query('DELETE FROM '.$tbl['admin_sessions'].' WHERE updated_time < "'.mysql_datetime($admin_timeout).'"');

// Select current session
$chk_session = $DB->fetch_array($DB->query('SELECT * FROM '.$tbl['admin_sessions'].' WHERE session_id="'.session_id().'"'));
if($chk_session)
{
	// If logged in
	if($chk_session["admin_id"])
	{
		// Get admin details
		$admin_session = $DB->fetch_array($DB->query('SELECT * FROM '.$tbl['admin_accounts'].' WHERE id="'.$chk_session["admin_id"].'"'));
		// Update session
		if($admin_session['active'])
		{
			$DB->query('UPDATE '.$tbl['admin_sessions'].' SET updated_time="'.mysql_datetime($time).'" WHERE session_id="'.session_id().'"');
			$admin_session['admin_id'] = $admin_session['id'];
		}
		// Delete session
		else 
			$DB->query('DELETE FROM '.$tbl['admin_sessions'].' WHERE session_id="'.session_id().'"');
	}
	// In not logged in
	else
		$DB->query('UPDATE '.$tbl['admin_sessions'].' SET updated_time="'.mysql_datetime($time).'" WHERE session_id="'.session_id().'"');
}
else
{
	// Insert new session
	$DB->query('INSERT INTO '.$tbl['admin_sessions'].'
				(session_id,admin_ip,admin_agent,updated_time) 
				VALUES  (
					"'.session_id().'",
					"'.$_SERVER['REMOTE_ADDR'].'",
					"'.$_SERVER['HTTP_USER_AGENT'].'",
					"'.mysql_datetime($time).'"
				) ');
}

// Get admin privilege
// Action: 1=view,2=add,3=edit,4=delete,5=publish
// Result: $privilege['module_name'] = 'viewaddeditdeletepublish';
if(!$admin_session['is_super_admin'])
{
	$arr_page_privi = explode("/",$admin_session["privilege"]);
	foreach ($arr_page_privi as $arr_id => $page_privi)
	{
		list($vpage,$vprivi) = explode(":",$page_privi);
		$privilege[$vpage] = replace_privilege($vprivi);;
		//echo $vpage.'=>'.replace_privilege($vprivi).'<br/>';
	}
}

// If admin not login, sent to login page
if(!$admin_session["admin_id"] && !defined('ADMIN_LOGIN'))
{
	echo '<script> parent.location=\'login.php\'; </script>';
	echo '<br/><div align="center">You have not logged in ... </div>';
	exit;
}

if(defined('ADMIN_LOGIN'))
{
	// Insert default data
	index_insert_default_data();
	index_check_license();
}
?>